package cs294s.liveradar;

import android.app.ListActivity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ListView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import cs294s.liveradar.service.FeedManager;

public class FeedList extends ListActivity{

	FeedManager mFeedManager;
	protected static final int CONTEXTMENU_DELETEITEM = 0;
    
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
	  //ignore orientation change
	  super.onConfigurationChanged(newConfig);
	}

	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
	    super.onCreate(savedInstanceState);
	    mFeedManager = FeedManager.getInstance(this);
	    
        setListAdapter(new FeedAdapter(this));
        
        // Theme.Light sets a background on our list.

        ListView listView = getListView();
        listView.setBackgroundDrawable(null);

        mFeedManager.updateFeeds();
        
		/* Add Context-Menu listener to the ListView. */
		listView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {

			@Override
			public void onCreateContextMenu(ContextMenu menu, View v,
					ContextMenuInfo menuInfo) {
				menu.setHeaderTitle("ContextMenu");
				menu.add(0, CONTEXTMENU_DELETEITEM, 0, "Edit this filter");
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
		/* Switch on the ID of the item, to get what the user selected. */
		switch (item.getItemId()) {
			case CONTEXTMENU_DELETEITEM:
				int position = info.position;
				
				Log.d("RADAR", "FILTER POSITION: " + position);
				 Intent i = new Intent(this, FilterEditForm.class);
			     i.putExtra("filterIndex", position);
			     startActivity(i);
				
				return true; /* true means: "we handled the event". */
		}
		return false;
	}

	
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    	/*Go to list of Blips for a particular feed*/
        Intent i = new Intent(this, BlipList.class);
        i.putExtra("feedPosition", position);
        startActivity(i);
    }   
}
